import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    cartList: [], // 购物车的数据
  },
  getters: {
  },
  mutations: {
    addCart(state,val){ // 加入购物车的方法
      let obj = { 
        buyNum: 1,
        id: val.id,
        url: val.cover_url,
        title: val.title,
        price: val.price
       }
      let index = state.cartList.findIndex(item => item.id == val.id);
      if (index > -1) {
        state.cartList[index].buyNum++;
      } else {
        state.cartList.push(obj);
      }
    },
    del(state,i){ // 购物车数据的删除
      state.cartList.splice(i,1);
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ]
})
